<?php
/**
 * Created by PhpStorm.
 * User: iraklis
 * Date: 1/12/14
 * Time: 11:56 PM
 */

session_start();
if (!isset($_SESSION["level"])) header("location:login.php");

?>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <link rel="shortcut icon" href="./files/img/favicon.ico">
    <title>RemindMe - Edit reminder</title>
</head>


<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

<style type="text/css">
    @import "style2.css";
</style>

<script type="text/javascript" src="./scripts.js"></script>

<?php
include 'sql.php';
?>

<body>

<div id="access">
    <?php echo showRightPanel(); ?>
</div> <!--End of access-->

<!--
Start Header Area
-->
<div id="header">
<?php showMenu(); ?>
</div><!-- End of HEADER -->

<!--
 Start Left Panel Area
 -->
<?php showLeftPanel($_SESSION['level'], $_SESSION['user']); ?>
<?php
    $event_id = $_GET['event_id'];
    $query = mysql_query("SELECT * FROM REMINDERS_RECURRANCY WHERE event_id='$event_id'");
    $event = mysql_fetch_assoc($query);
    $parent_id = $event['parent_id'];
    $query2 = mysql_query("SELECT * FROM REMINDERS WHERE parent_id='$parent_id'");
    $remindersArray = mysql_fetch_assoc($query2);

?>
<!--
End of Left_panel
-->

<!--
 Start Container Area
 -->

<div id="container">
    <div id="content"><!-- Content Area-->

        <form id="event">
            <?php showReminder($_SESSION['u_id'], $event['event_id']);?>
            <input type="button" name="edit-btn" id="edit-btn" value="Edit">
            <input type="button" name="delete-btn" id="delete-btn" value="Delete">
        </form>
        <div id="edit-event" class="<?php echo $event['event_id'] . "-" . $event['parent_id']; ?>" title="Edit Event" style="display: none">
            <form action="" id ="edit-event-form" name="edit-event-form">
                <label for="title">Title</label>
                <input type="text" name="title" id="title" value="<?php echo  $event['title']; ?>"/><br><br>

                <label for="edit-date">Date</label>
                <input type="datetime" name="event-date" id="event-date" value="<?php echo trim($event['start'], " "); ?>" tabindex="-1" /><br><br>

                <label for="edit-start-time">Start Time</label>
                <select name="start-time" id="start-time">
                    <option value="1">1 am</option>
                    <option value="2">2 am</option>
                    <option value="3">3 am</option>
                    <option value="4">4 am</option>
                    <option value="5">5 am</option>
                    <option value="6">6 am</option>
                    <option value="7">7 am</option>
                    <option value="8">8 am</option>
                    <option value="9">9 am</option>
                    <option value="10">10 am</option>
                    <option value="11">11 am</option>
                    <option value="12">12 pm</option>
                    <option value="13">1 pm</option>
                    <option value="14">2 pm</option>
                    <option value="15">3 pm</option>
                    <option value="16">4 pm</option>
                    <option value="17">5 pm</option>
                    <option value="18">6 pm</option>
                    <option value="19">7 pm</option>
                    <option value="20">8 pm</option>
                    <option value="21">9 pm</option>
                    <option value="22">10 pm</option>
                    <option value="23">11 pm</option>
                    <option value="00">12 am</option>
                </select>
                <select name="start-mins" id="start-mins">
                    <option value="00">0 min</option>
                    <option value="5">5 min</option>
                    <option value="10">10 min</option>
                    <option value="15">15 min</option>
                    <option value="20">20 min</option>
                    <option value="25">25 min</option>
                    <option value="30">30 min</option>
                    <option value="35">35 min</option>
                    <option value="40">40 min</option>
                    <option value="45">45 min</option>
                    <option value="50">50 min</option>
                    <option value="55">55 min</option>
                </select><br><br>

                <label for="edit-end-time">End Time</label>
                <select name="end-time" id="end-time">
                    <option value="1">1 am</option>
                    <option value="2">2 am</option>
                    <option value="3">3 am</option>
                    <option value="4">4 am</option>
                    <option value="5">5 am</option>
                    <option value="6">6 am</option>
                    <option value="7">7 am</option>
                    <option value="8">8 am</option>
                    <option value="9">9 am</option>
                    <option value="10">10 am</option>
                    <option value="11">11 am</option>
                    <option value="12">12 pm</option>
                    <option value="13">1 pm</option>
                    <option value="14">2 pm</option>
                    <option value="15">3 pm</option>
                    <option value="16">4 pm</option>
                    <option value="17">5 pm</option>
                    <option value="18">6 pm</option>
                    <option value="19">7 pm</option>
                    <option value="20">8 pm</option>
                    <option value="21">9 pm</option>
                    <option value="22">10 pm</option>
                    <option value="23">11 pm</option>
                    <option value="00">12 am</option>
                </select>
                <select name="end-mins" id="end-mins">
                    <option value="00">0 min</option>
                    <option value="5">5 min</option>
                    <option value="10">10 min</option>
                    <option value="15">15 min</option>
                    <option value="20">20 min</option>
                    <option value="25">25 min</option>
                    <option value="30">30 min</option>
                    <option value="35">35 min</option>
                    <option value="40">40 min</option>
                    <option value="45">45 min</option>
                    <option value="50">50 min</option>
                    <option value="55">55 min</option>
                </select><br><br>
                <label for="location">Location</label><br>
                <input type="text" name="location" id="location" value="<?php echo $remindersArray['location'] ?>"  /><br><br>
                <label for="description">Description</label><br>
                <input type="text" name="description" id="description" value="<?php echo $remindersArray['description'] ?>"  /><br><br>
                <div id="repeat" style="display: none">
                    <input type="hidden" name="repeats" id="repeats" value="<?php echo $remindersArray['repeats_freq']?>">
                </div>
            </form>
        </div>
        <div id="delete-event" class="<?php echo $remindersArray['repeats']; ?>" title="Delete Event" style="display: none">
            <form action="" id ="delete-event-form" name="delete-event-form" value="<?php echo $event['title']; ?>">
                <label for="title"></label>
                    <div id="non-repeating-event">
                         <p>
                            <label>Are you sure you want to delete this event?</label>
                            <input type="radio" name="delete-options" id="delete-this-one" style="display: none" checked>
                            <input type="hidden" id="<?php echo $event['parent_id']; ?>" class="delete-this-one">
                        </p>
                    </div>
                    <div id="repeating-event">
                        <p>
                            <label>Delete just this reminder?</label>
                            <input type="radio" name="delete-options" id="delete-this">
                            <input type="hidden" id="<?php echo $event['event_id']; ?>" class="delete-this">
                        </p>
                        <p>
                            <label>Delete all reminders?</label>
                            <input type="radio" name="delete-options" id="delete-all">
                            <input type="hidden" id="<?php echo $event['parent_id']; ?>" class="delete-all">
                        </p>
                    </div>
            </form>
        </div> <!--end of delete-event-->
    </div><!--END of content-->
</div><!--END of container-->

<?php showFooter(); ?><!--END of footer-->
</body>

<script>
    $(function() {
        $("#event-date").datepicker(
            {
                dateFormat:"yy-mm-dd"
            });
    });
    $(document).ready(function() {
        $("#edit-btn").click(function() {
            var class_values = $("#edit-event").attr('class');
            var array = class_values.split('-');
            var eventID = array[0];
            var parentID = array[1];

             $("#edit-event").dialog({
             title: "Edit",
             resizable: false,
             height:450,
             width:300,
             modal: true,
             buttons: {
                 "OK": function() {
                     var title = $("#title").val();
                     var event_date = $("#event-date").val();
                     var start_time = $("#start-time").val();
                     var start_mins = $("#start-mins").val();
                     var start = start_time+':'+start_mins;
                     var end_time = $("#end-time").val();
                     var end_mins = $("#end-mins").val();
                     var end = end_time+':'+end_mins;
                     var repeat_freq = $("#repeats").val();
                     var location = $('#location').val();
                     var description = $('#description').val();

                     var dataString = 'event='+eventID+'&parent_ID='+parentID+'&title='+title+'&start_date='+event_date+'&start_time='+start+'&end_time='+end+'&location='+location+'&description='+description+'&repeats_freq='+repeat_freq;

                     alert(dataString);
                     var request = $.ajax({
                         type: "POST",
                         url: "sql.php",
                         data: dataString,
                         cache: false,
                         success: function(html) {
                         }

                     });
                     request.done(function( ) {
                         alert( "Edit succesfull :) ");
                         window.location.href = 'calendar.php';

                     });
                     request.fail(function() {
                         alert( "Edit failed :( ");
                         window.location.href = 'calendar.php';
                     });

                     $(this).dialog("close");
                 },
                 Cancel: function() {
                     $(this).dialog("close");
                 }//end of Cancel
             }//end of buttons
             });
        });

        $("#delete-btn").click(function() {
            var repeats = $("#delete-event").attr('class');
            if (repeats == 0) {
                document.getElementById("non-repeating-event").style.display = 'block';
                document.getElementById("repeating-event").style.display = 'none';
            }
            else {
                document.getElementById("non-repeating-event").style.display = 'none';
                document.getElementById("repeating-event").style.display = 'block';
            }
            $("#delete-event").dialog({
                 title: "Delete",
                 resizable: false,
                 height:150,
                 width:250,
                 modal: true,
                 buttons: {
                     "OK": function() {

                         if(document.getElementById('delete-this').checked) {
                             var eventID = $(".delete-this").attr('id');
                             var dataString = 'event_id='+eventID;
                         }
                         else if (document.getElementById('delete-all').checked) {
                             var parent_id = $(".delete-all").attr('id');
                             var dataString = 'parent_id='+parent_id;
                         }
                         else if (document.getElementById('delete-this-one').checked) {
                            var parent_id = $(".delete-this-one").attr('id');
                            var dataString = 'parent_id='+parent_id;
                         }

                         var request = $.ajax({

                             type: "POST",
                             url: "sql.php",
                             data: dataString,
                             cache: false,
                             success: function(html) {
                             }

                         });
                         request.done(function( ) {
                             alert( "Deletion succeeded :) ");
                             window.location.href = 'calendar.php';

                         });
                         request.fail(function() {
                             alert( "Deletion failed :( ");
                             window.location.href = 'calendar.php';
                         });

                         $(this).dialog("close");
                     },
                     Cancel: function() {
                         $(this).dialog("close");
                     }//end of Cancel
                 }//end of buttons
            });// emd of dialog
        });// end of delete-btn

    }); // end of ready
</script>

</html>